package com.sky.happygos.ui.activity.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sky.happygos.R;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.CreateGoodsE;
import com.sky.happygos.event.CreateShopE;
import com.sky.happygos.event.GoodsE;
import com.sky.happygos.event.GoodsListE;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.model.Goods;
import com.sky.happygos.model.Shop;
import com.sky.happygos.present.GoodsP;
import com.sky.happygos.present.ShopP;
import com.sky.happygos.present.UserP;
import com.sky.happygos.ui.activity.CreateGoodsA_;
import com.sky.happygos.ui.activity.CreateShopA_;
import com.sky.happygos.ui.activity.GoodsDetailA;
import com.sky.happygos.ui.activity.GoodsDetailA_;
import com.sky.happygos.ui.activity.ShopCreateA_;
import com.sky.happygos.ui.activity.adapter.GoodsAdapter;
import com.sky.happygos.ui.activity.adapter.ShopAdapter;
import com.sky.happygos.utils.SpacesItemDecoration;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import static com.sky.happygos.ui.activity.CreateGoodsA.EXTRA_CREATE_SHOPID;
import static com.sky.happygos.ui.activity.GoodsDetailA.EXTRA_GOODS_ID;

/**
 * Created by qqqqqqq on 2017/4/15.
 */
@EFragment(R.layout.fragment_goods)
public class GoodsF extends Fragment implements GoodsAdapter.MyItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    @ViewById(R.id.rv_shop)
    RecyclerView recyclerView;
    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.rl_add_shop)
    RelativeLayout addShop;
    @ViewById(R.id.swipe_container)
    SwipeRefreshLayout swipeRefreshLayout;
    @ViewById(R.id.header_left)
    TextView title;

    @Bean
    ShopP shopP;
    @Bean
    GoodsP goodsP;

    GoodsAdapter goodsAdapter;

    Shop shop;
    List<Goods> goods;

    @AfterViews
    public void init(){
        swipeRefreshLayout.setOnRefreshListener(this);

        goods = new ArrayList<>();
        goodsAdapter = new GoodsAdapter(getContext(),goods);
        goodsAdapter.setMyItemClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        recyclerView.addItemDecoration(new SpacesItemDecoration(2,50,true));
        recyclerView.setAdapter(goodsAdapter);

        loadView.start();
        shopP.getMyShop();

    }

    @Subscribe
    public void shopsResult(ShopE e){
        if(e.isSuccess){
            List<Shop> shops = e.data;
            if(shops != null && shops.size() > 0){
                addShop.setVisibility(View.INVISIBLE);
                shop = shops.get(0);
                title.setText(shop.name);
                goodsP.getGoods(shop.id);
            }else{
                loadView.dismiss();
                addShop.setVisibility(View.VISIBLE);
            }
        }else{
            loadView.dismiss();
            addShop.setVisibility(View.VISIBLE);
        }
    }

    @Subscribe
    public void CreateShopResult(CreateShopE e){
        if(e.isSuccess){
                addShop.setVisibility(View.INVISIBLE);
                shop = e.data;
                title.setText(shop.name);
                loadView.start();
                goodsP.getGoods(shop.id);
        }
    }

    @Subscribe
    public void CreateGoodsResult(CreateGoodsE e){
        shopP.getMyShop();
    }

    @Subscribe
    public void goodsResult(GoodsListE e){
        loadView.dismiss();
        swipeRefreshLayout.setRefreshing(false);
        if(e.isSuccess){
            goods = e.data;
            goodsAdapter.list = goods;
            goodsAdapter.notifyDataSetChanged();
        }else{
            Toast.makeText(getActivity(),"获取商品失败:"+e.error.message,Toast.LENGTH_SHORT).show();
        }
    }


    @Click(R.id.header_right)
    public void addGoods(){
        CreateGoodsA_.intent(getActivity()).extra(EXTRA_CREATE_SHOPID,shop.id).start();
    }

    @Click(R.id.rl_add_shop)
    public void addShop(){
        ShopCreateA_.intent(getActivity()).start();
    }

    @Override
    public void onItemClick(View view, int postion) {
        String goodsId = goods.get(postion).id;
        GoodsDetailA_.intent(getContext()).extra(EXTRA_GOODS_ID,goodsId).start();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRefresh() {
        shopP.getMyShop();
    }
}
